import PaletteProvider from 'bpmn-js/lib/features/palette/PaletteProvider'
import ElementFactory from 'bpmn-js/lib/features/modeling/ElementFactory.js'
import { assign } from 'min-dash'
import { createAction } from '../utils'
import rewritePaletteOptions from './rewritePaletteOptions'

class RewritePaletteProvider extends PaletteProvider {
  private readonly _palette: PaletteProvider
  private readonly _create: any
  private readonly _elementFactory: ElementFactory
  private readonly _spaceTool: any
  private readonly _lassoTool: any
  private readonly _handTool: any
  private readonly _globalConnect: any
  private readonly _translate: any
  private readonly _moddle: any
  constructor(palette, create, elementFactory, spaceTool, lassoTool, handTool, globalConnect) {
    super(palette, create, elementFactory, spaceTool, lassoTool, handTool, globalConnect, 2000)
    this._palette = palette
    this._create = create
    this._elementFactory = elementFactory
    this._spaceTool = spaceTool
    this._lassoTool = lassoTool
    this._handTool = handTool
    this._globalConnect = globalConnect
  }
  getPaletteEntries() {
    const actions = {},
      create = this._create,
      elementFactory = this._elementFactory,
      lassoTool = this._lassoTool,
      handTool = this._handTool,
      globalConnect = this._globalConnect,
      spaceTool = this._spaceTool

    function createSqlTask(event) {
      const sqlTask = elementFactory.createShape({ type: 'miyue:SqlTask' })

      create.start(event, sqlTask)
    }

    function createSubprocess(event) {
      const subProcess = elementFactory.createShape({
        type: 'bpmn:SubProcess',
        x: 0,
        y: 0,
        isExpanded: true
      })

      const startEvent = elementFactory.createShape({
        type: 'bpmn:StartEvent',
        x: 40,
        y: 82,
        parent: subProcess
      })

      create.start(event, [subProcess, startEvent], {
        hints: {
          autoSelect: [startEvent]
        }
      })
    }

    function createParticipant(event) {
        create.start(event, elementFactory.createParticipantShape());
    }

    assign(actions, {
      'hand-tool': {
        group: 'tools',
        className: 'bpmn-icon-hand-tool',
        title: '手型工具',
        action: {
          click: function (event) {
            handTool.activateHand(event)
          }
        }
      },
      'lasso-tool': {
        group: 'tools',
        className: 'bpmn-icon-lasso-tool',
        title: '套索工具',
        action: {
          click: function (event) {
            lassoTool.activateSelection(event)
          }
        }
      },
      'space-tool': {
        group: 'tools',
        className: 'bpmn-icon-space-tool',
        title: '空间工具',
          click: function(event) {
            spaceTool.activateSelection(event);
          }
        },
      'global-connect-tool': {
        group: 'tools',
        className: 'bpmn-icon-connection-multi',
        title: '全局连线',
        action: {
          click: function (event) {
            globalConnect.toggle(event)
          }
        }
      },
      'tool-separator': {
        group: 'tools',
        separator: true
      },
      'create.start-event': createAction(
        elementFactory,
        create,
        'bpmn:StartEvent',
        'events',
        'bpmn-icon-start-event-none',
        '开始事件'
      ),
      'create.end-event': createAction(
        elementFactory,
        create,
        'bpmn:EndEvent',
        'events',
        'bpmn-icon-end-event-none',
        '结束事件'
      ),
      'create.intermediate-event': createAction(
        elementFactory,
        create,
        'bpmn:IntermediateThrowEvent', 
        'event', 
        'bpmn-icon-intermediate-event-none',
        '中间事件'
      ),
      'create.exclusive-gateway': createAction(
        elementFactory,
        create,
        'bpmn:ExclusiveGateway',
        'gateway',
        'bpmn-icon-gateway-none',
        '网关'
      ),
      'create.user-task': createAction(
        elementFactory,
        create,
        'bpmn:UserTask',
        'activity',
        'bpmn-icon-user-task',
        '用户任务'
      ),
      'create.data-object': createAction(
        elementFactory,
        create,
        'bpmn:DataObjectReference', 
        'data-object', 
        'bpmn-icon-data-object',
        '数据对象'
      ),
      'create.data-store': createAction(
        elementFactory,
        create,
        'bpmn:DataStoreReference',
        'data-store', 
        'bpmn-icon-data-store',
        '数据存储'
      ),
      'create.subprocess-expanded': {
        group: 'activity',
        className: 'bpmn-icon-subprocess-expanded',
        title: '子流程',
        action: {
          dragstart: createSubprocess,
          click: createSubprocess
        }
      },
      'create.participant-expanded': {
        group: 'collaboration',
        className: 'bpmn-icon-participant',
        title: '参与者',
        action: {
          dragstart: createParticipant,
          click: createParticipant
        }
      },
      'create.group': createAction(
        elementFactory,
        create,
        'bpmn:Group', 
        'artifact', 
        'bpmn-icon-group',
        '分组'
      ),
      'create.api-service-task': createAction(
        elementFactory,
        create,
        'bpmn:ServiceTask', 
        'activity', 
        'bpmn-icon-service-task',
        '调用接口任务',
        { taskType: 'ApiServiceTask', delegateExpression: "${flowableCallInterfaceDelegate}" }
      ),
      'create.set-variable-service-task': createAction(
        elementFactory,
        create,
        'bpmn:ServiceTask', 
        'activity', 
        'bpmn-icon-service-task',
        '设置变量任务',
        { taskType: 'SetVariableServiceTask', delegateExpression: "${flowableSetVariablesDelegate}" }
      ),
      'create.output-variable-service-task': createAction(
        elementFactory,
        create,
        'bpmn:ServiceTask', 
        'activity',
        'bpmn-icon-service-task',
        '输出变量任务',
        { taskType: 'OutputVariableServiceTask', delegateExpression: "${flowableOutputVariableDelegate}" }
      ),
    })

    // for (const key in actions) {
    //   if (rewritePaletteOptions[key] !== undefined) {
    //     if (typeof rewritePaletteOptions[key] == 'function') {
    //       actions[key] = rewritePaletteOptions[key](handTool)
    //     }
    //   } else {
    //     delete actions[key]
    //   }
    // }
    return actions
  }
}

RewritePaletteProvider.$inject = [
  'palette',
  'create',
  'elementFactory',
  'spaceTool',
  'lassoTool',
  'handTool',
  'globalConnect'
]

export default RewritePaletteProvider
